$(function(){
	//保存新收货地址
	var $btnAddressSave = $('#btnAddressSave');
	$btnAddressSave.click(function(){
		
		address_save();
	});
	
	//选择地址
	var $tabDetail = $('#tab-detail');
	if(!$('#address_0').attr('checked')){
		$tabDetail.hide();
	}
	
	$("input[name='address_id']").click(address_display);
	
	//提交订单
	var $btnSubmit = $('#btnSubmit');
	$btnSubmit.click(function(){
		$(this).attr('disabled',true);
		order_submit();
		$(this).attr('disabled',false);
	});
	
	//验证并使用
	var $bonus_check_use = $('#bonus_check_use');
	$bonus_check_use.click(function(){
		bonusCheckUse();
	});
	
	//取消
	var $bonus_cancel_use = $('#bonus_cancel_use');
	$bonus_cancel_use.click(function(){
		bonusCancelUse();
	});
	
	//防伪码
	var $sp_bonus_pwd 	= $('#sp_bonus_pwd');
	var $bonus_pwd		= $('#bonus_pwd');
	
	$sp_bonus_pwd.click(function(){
		
		var visible = $bonus_pwd.is(":visible");
		
		if(visible){
			$bonus_pwd.hide();
		}else{
			$bonus_pwd.show().focus();
		}
		
	});
	
});

//保存收货地址
function address_save(){
	
	var location 	= $('#location').val();
	var $address 	= $('#address');
	var $consignee 	= $('#consignee');
	var $mobile_phone= $('#mobile_phone');
	var consignee 	= $.trim($consignee.val());
	var mobile_phone= $.trim($mobile_phone.val());
	var address 	= $.trim($address.val());
	var address_id 	= $('input[name="address_id"]:checked').val();
	
	if(consignee == ''){
		alert('收货人不得为空');
		$consignee.focus();
		return false;
	}
	if(address == ''){
		alert('收货地址不得为空');
		$address.focus();
		return false;
	}
	if(mobile_phone == ''){
		alert('手机号码不得为空');
		$mobile_phone.focus();
		return false;
	}
	
	var params = {
		'address_id'	:address_id,
		'location'		:location,
		'consignee'		:consignee,
		'address'		:address,
		'mobile_phone'	:mobile_phone
	}
	
	$.post('/index.php/CheckOut/address_add',params,function(data){
		if(data.success){
			//清空数据
			set_address('','','');
			//隐藏
			$('#tab-detail').hide();
			//重新显示
			$('#div-address').load('/index.php/CheckOut/address_list',{},function(){
				$("input[name='address_id']").click(address_display);
			});
		}else{
			alert(data.message);
		}
	},'json');
}

//删除
function addressDel(id){
	if(!confirm('确认要删除此收货地址?')){
		return false;
	}
	
	$.post('/index.php/CheckOut/address_del',{'id':id},function(data){
		if(data.success){
			$('#li_address_' + id).remove();
		}else{
			alert(data.message);
		}
	},'json');
}

//默认
function addressDef(id){
	$('#tab-detail').hide();
	$.post('/index.php/CheckOut/address_def',{'id':id},function(data){
		if(data.success){
			$('#div-address').load('/index.php/CheckOut/address_list',{},function(){
				$("input[name='address_id']").click(address_display);
			});
		}else{
			alert(data.message);
		}
	},'json');
}

//设置收货地址
function set_address(address,consignee,mobile_phone){
	$('#address').val(address);
	$('#consignee').val(consignee);
	$('#mobile_phone').val(mobile_phone);
}

//隐藏或显示
function address_display(){
	
	var address_id = $("input[name='address_id']:checked").val();
	var $tabDetail = $('#tab-detail');
	
	if(address_id == 0){
		//新地址
		$tabDetail.show();
	}else{
		$tabDetail.hide();
	}
}
//订单提交
function order_submit(){
	
	var address_id		= $('input[name="address_id"]:checked').val();

	if(typeof address_id == 'undefined'){
		alert('请选择一个收货地址');
		return false;
	}else if(address_id == 0){
		alert('请点确认收货地址');
		return false;
	}
	
	var shipping_id		= $('#shipping_id').val();
	var delivery_time	= $('#delivery').val();
	if(delivery_time == ''){
		alert('请选择配送时间');
		return false;
	}
	
	var time			= $('#time').val();
	var pay_id			= $('input[name="pay_id"]:checked').val();
	var post_script		= $.trim($('#post_script').val());
		
	var params = {
		'address_id'		:address_id,
		'shipping_id'		:shipping_id,
		'delivery_time'		:delivery_time + ' ' + time,
		'pay_id'			:pay_id,
		'post_script'		:post_script
	};
	
	$.post('/index.php/CheckOut/order_submit',params,function(data){
		if(data.success){
			location.href = "/index.php/CheckOut/success";
		}else{
			alert(data.message);
		}
	},'json');
}

function bonusCheckUse(){
	var $bonus 			= $('#bonus');
	var bonus_sn 		= $.trim($bonus.val());
	var $bonus_pwd		= $('#bonus_pwd');
	var bonus_pwd		= $.trim($bonus_pwd.val());
	
	var params = {
		'bonus_sn'	:bonus_sn,
		'bonus_pwd'	:bonus_pwd
	};
	$.post('/index.php/CheckOut/bonus_use',params,function(data){
		if(data.success){
			alert(data.message);
			
			refreshMoney(data.data.bonus_money,'use');
			
			$bonus.attr('disabled','disabled');
			$bonus_pwd.attr('disabled','disabled');
			
			$('#bonus_check_use').hide();
			$('#bonus_cancel_use').show();
		}else{
			alert(data.message);
		}
	},'json');
}

//取消使用礼券
function bonusCancelUse(){

	var params = {};
	$.post('/index.php/CheckOut/bonus_cancel',params,function(data){
		if(data.success){
			alert(data.message);
		}else{
			alert(data.message);
		}
		
		refreshMoney(data.data.bonus_money,'cancel');
		
		$('#bonus,#bonus_pwd').attr('disabled',false);
		$('#bonus_pwd').val('');
		$('#bonus').val('').focus();
		$('#bonus_check_use').show();
		$('#bonus_cancel_use').hide();
		
	},'json');
}

//重新显示金额
function refreshMoney(bonus_money,act){
	
	bonus_money = parseFloat(bonus_money);
	
	var $bonus_money	= $('#bonus_money');
	var $need_pay_money	= $('#need_pay_money');
	
	
	var oldPayMoney 	= $('#need_pay_money').html();
		oldPayMoney 	= parseFloat(oldPayMoney.replace(/¥/g,''));

	var newBonusMoney = '0.00';
	var newPayMoney = '0.00';
	if(act == 'use'){
		newBonusMoney = parseFloat(bonus_money).toFixed(2);
		newPayMoney = parseFloat(oldPayMoney - bonus_money).toFixed(2);
	}else if(act == 'cancel'){
		newBonusMoney = '0.00';
		newPayMoney = parseFloat(oldPayMoney + bonus_money).toFixed(2);
	}
	
	$bonus_money.html('¥' + newBonusMoney);
	$need_pay_money.html('¥' + newPayMoney);
}

//使用礼券
function setBonus(bonus){
	
	if($("#bonus_cancel_use").is(":visible")){
		alert('请先取消使用原有礼券');
		return false;
	}
	
	$('#bonus').val(bonus);
	
	bonusCheckUse();
}